<template>
  <div>
    <el-dialog
      :title="'正在为 ['+ruleFormUserAdd.sysRoleName+' ] 授权'"
      :visible.sync="dialogFormVisibleUserAdd"
      :close-on-click-modal="false"
      @close="handleCloseUserAddClose"
    >
      <el-button-group>
        <el-button type="danger" size="medium" round :loading="loadingAuthor.loading" @click="submitFormUserAdd">确认授权</el-button>
      </el-button-group>
      <el-tree
        ref="tree"
        :data="dataTree"
        node-key="idStr"
        :show-checkbox="true"
        :highlight-current="true"
        :expand-on-click-node="true"
        :default-expanded-keys="defaultExpandedKeys"
        :default-checked-keys="defaultExpandedKeys"
        :props="defaultProps"
        @node-click="getCurrentKey"
      />
      <el-button-group>
        <el-button type="danger" size="medium" round :loading="loadingAuthor.loading" @click="submitFormUserAdd">确认授权</el-button>
      </el-button-group>
    </el-dialog>
  </div>
</template>

<script>
import { success } from '@/utils/tip'
import { aclsToRole } from '@/api/momo/SysRole'
export default {
  name: 'AclsToRole',
  props: {
    aclsToRoleToChild: {}
  },
  data() {
    return {
      dialogFormVisibleUserAdd: false,
      defaultExpandedKeys: [],
      currentNodeKey: [],
      dataTree: [],
      ruleFormUserAdd: {
        roleUuid: '',
        sysRoleName: ''
      },
      mapAcls: new Map(),
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      loadingAuthor: {
        loading: false,
        uuid: ''
      }
    }
  },
  watch: {
    aclsToRoleToChild: {
      deep: true,
      immediate: false,
      handler() {
        const acls = JSON.parse(JSON.stringify(this.aclsToRoleToChild))
        this.dataTree = acls.aclLevelRes
        this.defaultExpandedKeys = acls.defaultexpandedKeys
        this.dialogFormVisibleUserAdd = true
        this.ruleFormUserAdd.roleUuid = acls.roleUuid
        this.ruleFormUserAdd.sysRoleName = acls.sysRoleName
      }
    }
  },
  methods: {
    submitFormUserAdd(formName) {
      this.loadingAuthor.loading = true
      const aclsToRoleData = {
        roleUuid: this.ruleFormUserAdd.roleUuid,
        acls: this.$refs.tree.getCheckedNodes(false, true)
      }
      aclsToRole(aclsToRoleData).then(res => {
        res = res.data
        success(res)
        this.loadingAuthor.loading = false
        this.dialogFormVisibleUserAdd = false
        this.$emit('userAddToFather', '我是孩子')
      }).catch(errorEd => {
        this.loadingAuthor.loading = false
      })
    },
    resetFormUserAdd(formName) {
      this.ruleFormUserAdd.remark = ''
      this.$refs[formName].resetFields()
    },
    handleCloseUserAdd() {
      // this.dialogFormVisibleUserAdd = false
      // this.$emit('userAddToFather', '我是孩子')
    },
    getCurrentKey(value) {
    },
    handleCloseUserAddClose() {
      // this.dialogFormVisibleUserAdd = false
      // this.$emit('userAddToFather', '我是孩子')
    }
  }
}
</script>

<style scoped>

</style>
